<?php
 /*******************************************************************************
 * @filename: choose.ctp
 *
 * @author: immer_baldos
 *
 * @description: product chooser page
 *
 * @history:
 *
 *   VERSION   DATE                   BY          NOTES
 *  ---------------------------------------------------------------
 *     1.0     September 24, 2009     immer       initial implementation
 *
 ******************************************************************************/
?>

<script LANGUAGE="JavaScript">
function getCnt(pid) {
    var count = parseInt(prompt("How many do you want to buy?", ""));
    if (count>0){
        //expected arguments: $OrderId, $CustomerId, $ProductId, $ProductCnt) {
        var param = '../../transactions/oneentry/'+ <?php echo $order['Order']['id'] ?>;
        param += '/' + <?php echo $_SESSION['Auth']['User']['customer_id'] ?>;
        param += '/' + pid + '/' + count;
        window.location=param;
    }else{
        alert ("Input must be > 0.");
    }
}
</script>

<!-- ################################# PRODUCTS BOUGHT ################################# -->
<div class="orders view">
<h2><?php  __('Order '.$order['Order']['order_number']);  ?></h2><table cellpadding="0" cellspacing="0">
<tr>
	<th><?php echo _('name');?></th>
	<th><?php echo _('description');?></th>
	<th><?php echo _('item count');?></th>
	<th><?php echo _('unit price');?></th>
	<th><?php echo _('total');?></th>
	<th class="actions"><?php __('Actions');?></th>
</tr>
<?php if (!empty($transactions)): ?>
<?php
$i = 0;
$sum = 0;
foreach ($transactions as $transaction):
	$class = null;
	if ($i++ % 2 == 0) {
		$class = ' class="altrow"';
	}
?>
	<tr<?php echo $class;?>>
		<td>
			<?php echo $transaction['Product']['name']; ?>
		</td>
		<td>
			<?php echo $transaction['Product']['description']; ?>
		</td>
		<td>
			<?php echo $transaction['Transaction']['count']; ?>
		</td>
		<td>
			<?php echo number_format($transaction['Product']['price'],2); ?>
		</td>
		<td>
			<?php
			    echo number_format(($transaction['Product']['price']*$transaction['Transaction']['count']),2);
			    $sum += ($transaction['Product']['price']*$transaction['Transaction']['count']);
			?>
		</td>
		<td class="actions">
		    <?php echo $html->link(__('Delete', true), array('controller' => 'transactions', 'action' => 'delete', $transaction['Transaction']['id']), null, sprintf(__('Are you sure?', true), $transaction['Transaction']['id'])); ?>
		</td>
	</tr>
<?php endforeach; ?>
<td></td><td></td><td></td>
<td><b>Grand Total</b></td>
<td><b>
<?php
    echo number_format($sum,2);
?>
</b></td>
<?php endif; ?>
</table>
</div>

<div class="actions">
	<ul>
		<li><?php echo $html->link(__('Cancel Order', true), array('action' => 'delete', $order['Order']['id']), null, sprintf(__('Are you sure you want to cancel this order?', true), $order['Order']['id'])); ?> </li>
		<li><?php echo $html->link(__('Finalize Order', true), array('action' => 'finalize', $order['Order']['id'])); ?> </li>
	</ul>
</div>

<!-- ################################# PRODUCTS LIST################################# -->
<div class="products index">
<h3><?php __('Products');?></h3>
<p>
<table cellpadding="0" cellspacing="0">
<tr>
	<th><?php echo _('name');?></th>
	<th><?php echo _('description');?></th>
	<th><?php echo _('instock');?></th>
	<th><?php echo _('price');?></th>
	<th class="actions"><?php __(' ');?></th>
</tr>
<?php
$i = 1;
foreach ($products as $product):
	$class = null;
	if ($i++ % 2 == 0) {
		$class = ' class="altrow"';
	}
?>
	<tr<?php echo $class;?>>
		<td>
			<?php echo $product['Product']['name']; ?>
		</td>
		<td>
			<?php echo $product['Product']['description']; ?>
		</td>
		<td>
			<?php echo $product['Product']['instock']; ?>
		</td>
		<td>
			<?php echo number_format($product['Product']['price'],2); ?>
		</td>
		<td class="actions">
		<a href="javascript:getCnt( <?php echo $product['Product']['id'] ?> );">Buy</a>
		</td>
	</tr>
<?php endforeach; ?>
</table>
</div>
